package com.dcbeida.activity.stockdetail;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dcbeida.R;
import com.dcbeida.activity.BaseActivity;
import com.dcbeida.activity.inventory.InventoryActivity;
import com.dcbeida.adapter.KcpdbgAdapter;
import com.dcbeida.bean.Kcpdbg;
import com.dcbeida.widget.DataLoadDirection;
import com.dcbeida.widget.XListView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 库存盘点报告
 * Created by 150410 on 2017/10/26.
 */

public class KcpdbgActivity extends BaseActivity implements XListView.IXListViewListener, KcpdbgContract.View, AdapterView.OnItemClickListener {
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.rel_title1)
    RelativeLayout relTitle1;
    @Bind(R.id.myList)
    XListView myList;
    @Bind(R.id.ll_no_data)
    LinearLayout llNoData;

    private ArrayList<Object> datalist;
    private KcpdbgAdapter adapter;
    private KcpdbgPersentr mPresent;

    private boolean isEdit = false;
    private boolean isEnd = false;// 是否有更多
    private int Totle = 0;//
    private int pageindex = 0;
    private int pagesize = 10;
    int type = DataLoadDirection.Refresh;

    @Override
    protected int getLayoutResId() {
        return R.layout.kcpdbg_act;
    }

    @Override
    public void setData() {
        imgBack.setVisibility(View.VISIBLE);
        tvTitle.setText(R.string.kcpdbg);
        tvRight.setText(R.string.tjpd);
        datalist = new ArrayList<Object>();
        myList.setPullLoadEnable(false);
        myList.setPullRefreshEnable(true);
        myList.setXListViewListener(this);
        myList.setOnItemClickListener(this);
        myList.setVerticalScrollBarEnabled(false);// 去掉竖向滚动条
        adapter = new KcpdbgAdapter(mContext);
        myList.setAdapter(adapter);

        mPresent = new KcpdbgPersentr(this, this);
        mPresent.getList(pageindex, pagesize);

    }

    @OnClick({R.id.img_back, R.id.tv_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_right:
                showActivity(InventoryActivity.class,null);
                break;
        }
    }


    @Override
    public void onRefresh() {
        // 执行刷新任务
        type = DataLoadDirection.Refresh;
        mPresent.getList(0, pagesize);
    }

    @Override
    public void onLoadMore() {
        // 执行加载更多任务
        type = DataLoadDirection.LoadMore;
        if (isEnd) {
            mPresent.getList(pageindex + 1, pagesize);
        }
    }



    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Kcpdbg.ListBean bean = (Kcpdbg.ListBean)adapter.getItem(position-1);
        Bundle bundle = new Bundle();
        bundle.putSerializable("Kcpdbg.ListBean",bean);
        showActivity(CyxqActivity.class,bundle);
    }


    @Override
    public void showTelTip(String msg) {

    }

    @Override
    public void showView(boolean hasData) {

    }

    @Override
    public void showList(ArrayList<Kcpdbg.ListBean> list) {
        if (list.size() < 10)
            isEnd = false;
        else
            isEnd = true;

        Totle = adapter.getmArrayList().size() + list.size();


        if (type == DataLoadDirection.Refresh) {
            myList.stopRefresh();
            pageindex = 0;
            adapter.clearList();
        } else if (type == DataLoadDirection.LoadMore) {
            myList.stopLoadMore();
            pageindex++;
        }

        if (isEnd) {
            myList.showFooterView();
            myList.setPullLoadEnable(true);
        } else {
            myList.hideFooterView();
            myList.setPullLoadEnable(false);
        }

        adapter.setmArrayList(list);

        if (0 == adapter.getCount()) {
            myList.setVisibility(View.GONE);
            llNoData.setVisibility(View.VISIBLE);
        } else {
            myList.setVisibility(View.VISIBLE);
            llNoData.setVisibility(View.GONE);
        }
    }


    @Override
    public void showData(Kcpdbg stockDetail) {

    }


    @Override
    public void setPresenter(KcpdbgContract.Persent presenter) {

    }
}
